package com.cn.jdia.tas.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.jdia.tas.context.SpringContextUtils;
import com.cn.jdia.tas.model.S_worklog;
import com.cn.jdia.tas.service.IWorklogService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

@Controller
@RequestMapping("/control")
public class WorklogController {

	@Resource
	private IWorklogService service = null;

	@RequestMapping("/insertlog")
	public String insertWorkLogData(HttpServletRequest request, HttpServletResponse response) throws IOException {

		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");

		Gson gson = new Gson();
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String employeeId = map.get("id");
		String workDate = map.get("date");
		String begTime = map.get("begtime");
		String endTime = map.get("endTime");
		String content = map.get("content");

		DateTime now = new DateTime();
		String creat = now.toString("yyyyMMddHHmmss");

		S_worklog log = new S_worklog();
		log.setCreattime(creat);
		log.setEmployeeid(employeeId);
		log.setWorkdate(workDate);
		log.setBegtime(begTime);
		log.setEndtime(endTime);
		log.setContent(content);

//		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
//		service = (IWorklogService) w_context.getBean("worklogService");
		service = (IWorklogService)SpringContextUtils.getContext().getBean("worklogService");
		
		int ret = service.insertWorkLog(log);
		String sret = String.valueOf(ret);

		out.write(sret);
		out.flush();
		out.close();

		return null;
	}

	@RequestMapping("/querytoplog")
	public String queryTopWorkLogDataByEmId(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");

		Gson gson = new Gson();
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String employeeId = map.get("id");

//		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
//		service = (IWorklogService) w_context.getBean("worklogService");
		
		service = (IWorklogService)SpringContextUtils.getContext().getBean("worklogService");
		
		List<S_worklog> worklist = service.getTopWorkLogByEmployeeId(employeeId);

		List<Map<String, String>> list = new ArrayList<>();
		for (S_worklog log : worklist) {
			Map<String, String> lmap = new HashMap<>();

			lmap.put("date", log.getWorkdate());
			lmap.put("begtime", log.getBegtime());
			lmap.put("endtime", log.getEndtime());
			lmap.put("content", log.getContent());

			list.add(lmap);
		}

		String retDate = gson.toJson(list);
		out.write(retDate);
		out.flush();
		out.close();

		return null;
	}
	
	
	@RequestMapping("/querydatelog")
	public String queryDateWorkLogDataByEmId(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();

		String data = request.getParameter("data");

		Gson gson = new Gson();
		Map<String, String> map = gson.fromJson(data, new TypeToken<Map<String, String>>() {
		}.getType());

		String employeeId = map.get("id");
		String begDate = map.get("begDate");
		String endDate = map.get("endDate");

//		ClassPathXmlApplicationContext w_context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
//		service = (IWorklogService) w_context.getBean("worklogService");
		
		service = (IWorklogService) SpringContextUtils.getContext().getBean("worklogService");
		
		List<S_worklog> worklist = service.getDateWorkLogByEmployeeId(employeeId, begDate, endDate);

		List<Map<String, String>> list = new ArrayList<>();
		for (S_worklog log : worklist) {
			Map<String, String> lmap = new HashMap<>();

			lmap.put("date", log.getWorkdate());
			lmap.put("begtime", log.getBegtime());
			lmap.put("endtime", log.getEndtime());
			lmap.put("content", log.getContent());

			list.add(lmap);
		}

		String retDate = gson.toJson(list);
		out.write(retDate);
		out.flush();
		out.close();

		return null;
	}
}
